/**
 *@program: getx_simple_code
 *@description:
 *@author: Ming
 *@create: 2023-12-27 17:05
 *@version v1.0
 */
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_simple_code/pages/obx/user.dart';

class ObxView extends StatelessWidget {
  ObxView({Key? key}) : super(key: key);
  var user = User(name: '小王', age: 20).obs;
  var count = 0.obs;
  var list = ['1','2'].obs;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("状态管理-obx"),
      ),
      body: Center(
        child: Column(
          children: [
            Obx(() {
              return Text(
                "count的值为: ${count}",
                style: TextStyle(color: Colors.red, fontSize: 30),
              );
            }),
            ElevatedButton(
                onPressed: () {
                  count.value++;
                  },
                child: Text("改变数值")),
            Obx(() {
              return Text(
                "数组=${list.value}",
                style: TextStyle(color: Colors.red, fontSize: 30),
              );
            }),
            ElevatedButton(
                onPressed: () {
                  list.add(Random().nextInt(10).toString());
                },
                child: Text("改变数组")),
            Obx(() {
              return Text(
                "姓名=${user.value.name},年龄=${user.value.age}",
                style: TextStyle(color: Colors.red, fontSize: 30),
              );
            }),
            ElevatedButton(
                onPressed: () {
                  user.update((user) {
                    user?.age++;
                  });
                },
                child: Text("改变对象")),
          ],
        ),
      )
    );
  }
}

